# ==============================
# _*_ coding:utf-8 _*_
# _*_ by:zizle     _*_
# _*_ at:20230424  _*_
# ==============================
from fastapi import APIRouter, Query, Body
from pydantic import BaseModel, validator
from response import APIResponse
from utils import datetime_utils
from constant.message import ShortFastMessage
from db_utils import BaseDatabaseConnection


class ShortMessage:

    def __init__(self, page: int = 1, page_size: int = 30):
        self.page = page
        self.page_size = page_size

    def get_short_message(self, msg_type: int):
        if msg_type not in ShortFastMessage.CATEGORIES:
            return APIResponse.validate_error(detail='查询的数据类型错误!')
        sql = """
            SELECT id,msg_time,msg_type,content FROM infor_short_message 
            WHERE msg_type=%s AND is_active=1 ORDER BY msg_time DESC;
        """
        with BaseDatabaseConnection() as conn:
            results = conn.query_paginator(sql, param=[msg_type],
                                           page=self.page, page_size=self.page_size)
        res_data = {
            'data': results['rows'],
            'page': results['page'],
            'page_size': results['page_size'],
            'total_count': results['total_count']
        }
        return APIResponse.success(data=res_data)


short_message_api = APIRouter()


@short_message_api.get('/')
def get_short_message(msg: int = Query(...), page: int = Query(1), page_size: int = Query(30)):
    return ShortMessage(page=page, page_size=page_size).get_short_message(msg_type=msg)

